package com.resou.reader.bookshelf.readhistory.datasupport;

import android.content.ContentValues;

import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.api.entry.BookListBean;
import com.resou.reader.utils.log.DLog;

import org.litepal.LitePal;

/**
 * Created by ${Garrett} on 2018/9/16.
 * Contact me krouky@outlook.com
 */
public class DataSupport {
    private static DataSupport ourInstance;

    public static DataSupport getInstance() {
        if (ourInstance == null) {
            synchronized (DataSupport.class) {
                if (ourInstance == null) {
                    ourInstance = new DataSupport();
                }
            }
        }
        return ourInstance;
    }

    private DataSupport() {
    }

    /**
     * 保存收藏(书架)
     *
     * @param bean               bookDetailBean
     * @param historyChapterName 阅读的历史章节名称
     * @param chapterIndex       章节索引
     * @param charIndex          文字在章节中的索引
     */
    public synchronized void saveCollection(BookDetailBean bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {
        BookCollection bookCollection = new BookCollection();

        bookCollection.setAuthor(bean.getAuthorName());
        bookCollection.setBookId(String.valueOf(bean.getId()));
        bookCollection.setCategoryName(bean.getCategoryName());
        bookCollection.setCreateTime(bean.getCreateTime());
        bookCollection.setDescription(bean.getNovelDescription());
        bookCollection.setKeywords(bean.getKeywords());
        bookCollection.setName(bean.getNovelName());
        bookCollection.setParentCategoryName(bean.getParentCategoryName());
        bookCollection.setPicUrl(bean.getCoverUrl());
        bookCollection.setSerialStatus(bean.getSerialStatus());
        bookCollection.setTotalWord(bean.getTotalWord());
        bookCollection.setUpdateTime(bean.getUpdateTime());
        bookCollection.setShareUrl(bean.getShareUrl());
        bookCollection.setChapterSum(bean.getChapterNum());
        //不免费
//        if (bean.getFreeType() == 0) {
//            bookCollection.setFreeStatus(true);
//        } else {
//            bookCollection.setFreeStatus(false);
//        }
        bookCollection.setFreeStatus(bean.getFreeType());
        bookCollection.setUserUpdateTime(System.currentTimeMillis());
        bookCollection.setHistoryChapterName(historyChapterName);
        bookCollection.setHistoryChapterId(historyChapterId);
        bookCollection.setChapterIndex(chapterIndex);
        bookCollection.setPageIndex(pageIndex);
        bookCollection.setCharIndex(charIndex);
        bookCollection.save();
    }

    /**
     * 保存收藏(书架)
     *
     * @param bean               bookDetailBean
     * @param historyChapterName 阅读的历史章节名称
     * @param chapterIndex       章节索引
     * @param charIndex          文字在章节中的索引
     */
    public synchronized void saveCollection(BookListBean.ListBookBean.BooksBean bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {
        BookCollection bookCollection = new BookCollection();

        bookCollection.setAuthor(bean.getNovelAuthor());
        bookCollection.setBookId(String.valueOf(bean.getNovelId()));
        bookCollection.setCategoryName(bean.getNovelCategory());
        bookCollection.setDescription(bean.getNovelRecommend());
        bookCollection.setKeywords(bean.getNovelWords() + "");
        bookCollection.setName(bean.getNovelName());
        bookCollection.setPicUrl(bean.getNovelCover());
        bookCollection.setSerialStatus(bean.getSeries());
        //不免费
        bookCollection.setFreeStatus(1);
        bookCollection.setUserUpdateTime(System.currentTimeMillis());
        bookCollection.setHistoryChapterName(historyChapterName);
        bookCollection.setHistoryChapterId(historyChapterId);
        bookCollection.setChapterIndex(chapterIndex);
        bookCollection.setPageIndex(pageIndex);
        bookCollection.setCharIndex(charIndex);

        boolean b = bookCollection.save();
    }

    private static final String TAG = "DataSupport";

    /**
     * 保存阅读记录
     *
     * @param bean               bookDetailBean
     * @param historyChapterName 阅读的历史章节名称
     * @param chapterIndex       章节索引
     * @param charIndex          文字在章节中的索引
     */
    public synchronized ReaderHistory saveReadHistory(BookDetailBean bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {
        ReaderHistory readerHistory = new ReaderHistory();

        readerHistory.setAuthor(bean.getAuthorName());
        readerHistory.setBookId(String.valueOf(bean.getId()));
        readerHistory.setCategoryName(bean.getCategoryName());
        readerHistory.setCreateTime(bean.getCreateTime());
        readerHistory.setDescription(bean.getNovelDescription());
        readerHistory.setKeywords(bean.getKeywords());
        readerHistory.setName(bean.getNovelName());
        readerHistory.setParentCategoryName(bean.getParentCategoryName());
        readerHistory.setPicUrl(bean.getCoverUrl());
        readerHistory.setSerialStatus(bean.getSerialStatus());
        readerHistory.setTotalWord(bean.getTotalWord());
        readerHistory.setUpdateTime(bean.getUpdateTime());
        readerHistory.setShareUrl(bean.getShareUrl());
        readerHistory.setChapterSum(bean.getChapterNum());
        readerHistory.setDeleted(false);
        readerHistory.setUserUpdateTime(System.currentTimeMillis());
        readerHistory.setHistoryChapterId(historyChapterId);
        readerHistory.setHistoryChapterName(historyChapterName);
        readerHistory.setChapterIndex(chapterIndex);
        readerHistory.setPageIndex(pageIndex);
        readerHistory.setCharIndex(charIndex);
        readerHistory.setZsBookId(bean.getZsBookId());
        readerHistory.setLastChapterName(bean.getUpdateChapterName());

        boolean isSuccess = readerHistory.save();
        if (isSuccess)
            return readerHistory;
        else
            return null;
    }

    /**
     * 保存阅读记录
     * 详情页点击加入书架--> 退出详情页到书架-->点击书架上的书到阅读器 --> 阅读器保存阅读记录
     *
     * @param bean               bookDetailBean
     * @param historyChapterName 阅读的历史章节名称
     * @param chapterIndex       章节索引
     * @param charIndex          文字在章节中的索引
     */
    public synchronized ReaderHistory saveReadHistory(BookCollection bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {
        ReaderHistory readerHistory = new ReaderHistory();

        readerHistory.setAuthor(bean.getAuthor());
        readerHistory.setBookId(String.valueOf(bean.getBookId()));
        readerHistory.setCategoryName(bean.getCategoryName());
        readerHistory.setCreateTime(bean.getCreateTime());
        readerHistory.setDescription(bean.getDescription());
        readerHistory.setKeywords(bean.getKeywords());
        readerHistory.setName(bean.getName());
        readerHistory.setParentCategoryName(bean.getParentCategoryName());
        readerHistory.setPicUrl(bean.getPicUrl());
        readerHistory.setSerialStatus(bean.getSerialStatus());
        readerHistory.setTotalWord(bean.getTotalWord());
        readerHistory.setUpdateTime(bean.getUpdateTime());
        readerHistory.setShareUrl(bean.getShareUrl());
        readerHistory.setChapterSum(bean.getChapterSum());
        readerHistory.setDeleted(false);
        readerHistory.setUserUpdateTime(System.currentTimeMillis());
        readerHistory.setHistoryChapterId(historyChapterId);
        readerHistory.setHistoryChapterName(historyChapterName);
        readerHistory.setChapterIndex(chapterIndex);
        readerHistory.setPageIndex(pageIndex);
        readerHistory.setCharIndex(charIndex);
        readerHistory.setZsBookId(bean.getZsBookId());
        readerHistory.setLastChapterName(bean.getLastChapterName());

        boolean isSuccess = readerHistory.save();
        if (isSuccess)
            return readerHistory;
        else
            return null;
    }

    /**
     * 更新书架记录
     *
     * @param bean
     * @param historyChapterName
     * @param historyChapterId
     * @param chapterIndex
     * @param pageIndex
     * @param charIndex
     */
    public synchronized void updateCollection(BookCollection bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {
        BookCollection bookCollection = new BookCollection();
        bookCollection.setChapterSum(bean.getChapterSum());
        bookCollection.setHistoryChapterName(historyChapterName);
        bookCollection.setChapterIndex(chapterIndex);
        bookCollection.setPageIndex(pageIndex);
        bookCollection.setCharIndex(charIndex);
        bookCollection.setHistoryChapterId(historyChapterId);
        bookCollection.setUserUpdateTime(System.currentTimeMillis());
        bookCollection.saveOrUpdate("bookid = ?", bean.getBookId());
        DLog.d("HomeBookShelfFragment", "historyChapterId:" + historyChapterId + "updated : " + bookCollection.getChapterIndex() + "chapterindex" + chapterIndex + "charIndex:" + charIndex);
    }

    /**
     * 更新历史记录
     *
     * @param bean
     * @param historyChapterName
     * @param historyChapterId
     * @param chapterIndex
     * @param pageIndex
     * @param charIndex
     */
    public synchronized void updateHistory(ReaderHistory bean, String historyChapterName, String historyChapterId, int chapterIndex, int pageIndex, int charIndex) {

        try {
            ReaderHistory history = new ReaderHistory();
            history.setChapterSum(bean.getChapterSum());
            history.setHistoryChapterName(historyChapterName);
            history.setChapterIndex(chapterIndex);
            history.setPageIndex(pageIndex);
            history.setCharIndex(charIndex);
            history.setHistoryChapterId(historyChapterId);
            history.setUserUpdateTime(System.currentTimeMillis());
            history.setDeleted(false);
            history.saveOrUpdate("bookId = ?", bean.getBookId());
            DLog.d("history", "updated : " + history.getChapterIndex() + "chapterindex" + chapterIndex + "charIndex:" + charIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
